package com.customer.mall.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.customer.mall.domain.User;
import com.customer.mall.domain.vo.UserVO;
import com.customer.mall.util.ResponseResult;
import com.customer.mall.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author BaiGGG
 * @since 2023-06-25
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private IUserService userService;

    @PostMapping("/login")
    public ResponseResult<User> login(String phone, String password, String verifyCode, HttpSession session){
        return userService.login(phone, password, verifyCode, session);
    }

    @PostMapping("/reg")
    public ResponseResult<String> reg(String phone, String password1, String password2, String verifyCode, HttpSession session){
        return userService.reg(phone, password1, password2, verifyCode, session);
    }

    @GetMapping("/logout")
    public ResponseResult<String> logout(HttpSession session){
        return userService.logout(session);
    }

    @GetMapping("/isLogin")
    public ResponseResult<UserVO> isLogin(HttpSession session){
        return userService.isLogin(session);
    }

    @GetMapping("/admin/list")
    public ResponseResult<Page<UserVO>> pageUser(@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "5") Integer pageSize){
        return userService.pageUser(pageNum, pageSize);
    }

    @GetMapping("/admin/{id}")
    public ResponseResult<UserVO> getUserById(@PathVariable Long id){
        UserVO user = userService.getUserById(id);
        return Objects.isNull(user) ? ResponseResult.failResult() : ResponseResult.okResult(user);
    }

    @PostMapping("/admin/update")
    public ResponseResult<String> updateUser(@RequestBody User user){
        return userService.updateUser(user);
    }

    @PostMapping("/admin/delete")
    public ResponseResult<String> deleteUser(@RequestBody List<Integer> ids){
        return userService.removeBatchByIds(ids) ? ResponseResult.okResult() : ResponseResult.failResult();
    }

}
